//
//  SecondViewController.swift
//  Hello2018
//
//  Created by 王小林 on 2017/8/31.
//  Copyright © 2017年 王小林. All rights reserved.
//

import UIKit
import Flutter
import AVFoundation
import Photos
import AssetsLibrary
import MobileCoreServices
import FlutterPluginRegistrant

class SecondViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func loadFutter(_ sender: UIButton) {
        let flutterViewController = FlutterViewController()
        GeneratedPluginRegistrant.register(with: flutterViewController);
//        flutterViewController.setInitialRoute("route1")
        
        
        let methodChannl = FlutterMethodChannel.init(name: "com.sxb.flutterdemo/permisssioncamera", binaryMessenger: flutterViewController)
        methodChannl.setMethodCallHandler({
            (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
            // Handle battery messages.
            if call.method == "requestCamera" {
                let status:AVAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
                switch status {
                case AVAuthorizationStatus.authorized:
                    result(true)
                    break;
                case AVAuthorizationStatus.denied:
                    AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: {
                        (granted: Bool) in
                        if granted {
                            result(true)
                        } else {
                            result(false)
                        }
                    })
                    break;
                case AVAuthorizationStatus.restricted:
                    result(false)
                    break;
                default:
                    result(false)
                    break;
                }
            }else{
                result(false)
            }
        })
        
        
        
        
        self.present(flutterViewController, animated: false, completion: nil)
    }
    @IBAction func takePhoto(_ sender: Any) {
//        let avStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video);
//
//        if avStatus == AVAuthorizationStatus.authorized {
//            let useable = UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera);
//            if useable {
//                let photoPicker = UIImagePickerController()
//                photoPicker.delegate = self
//                photoPicker.allowsEditing = false
//                photoPicker.sourceType = PHAssetSourceType.typeUserLibrary;
//                present(photoPicker, animated: true, completion: nil)
//            }
//        }else {
//
//        }
        
        addIcon()
    }
    
    func addIcon()  {
        
        let alertAction = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
        alertAction.addAction(UIAlertAction.init(title: "获取相机", style: .default, handler: { (alertCamera) in
            
            let picker:UIImagePickerController = UIImagePickerController()
            picker.sourceType = .camera
            picker.delegate = self as UIImagePickerControllerDelegate & UINavigationControllerDelegate
            picker.allowsEditing = true
            if UIImagePickerController.isSourceTypeAvailable(.camera){
                
                self.present(picker, animated: true, completion: { () -> Void in
                    
                })
            }else{
//                MBProgressHUD.showForError(to: self.view, text: "为授予防问相机权限", completion: nil)
            }
        }))
        
        alertAction.addAction(UIAlertAction.init(title: "获取相册", style:.default, handler: { (alertPhpto) in
            
            let picker:UIImagePickerController = UIImagePickerController()
            picker.sourceType = .photoLibrary
            picker.delegate = self as UIImagePickerControllerDelegate & UINavigationControllerDelegate
            picker.allowsEditing = true
            
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
                
                self.present(picker, animated: true, completion: {
                    () -> Void in
                })
            }else{
//                MBProgressHUD.showForError(to: self.view, text: "为授予防问相册权限", completion: nil)
            }
        }))
        
        alertAction.addAction(UIAlertAction.init(title: "取消", style: .cancel, handler: { (alertCancel) in
            
        }))
        
        self.present(alertAction, animated: true, completion: nil)
    }
    
    // MARK: ImagePicker Delegate 选择图片成功后代理
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
//        if let chosenImage =  info[UIImagePickerControllerOriginalImage] as? UIImage {
//            picker.dismiss(animated: true) {
//                self.iconImage.image = chosenImage
//            }
//            //处理传入后台
//        }
        dismiss(animated: true, completion: nil)
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
    
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        
    }

}

